/*
 * app.js -- Application JavaScripts
 */

/**
 * Scripts to run when the page is loaded
 */
$(function() {
	
	// Login Form
	if ($('#login_form').length > 0) {
		$('#login_form\\:loginPassword').keypress(function(event) {
			if (event.charCode == 13) {
				$('#login_form\\:loginSubmit').click();
			}
		});
		$('#login_form\\:loginEmail').focus();
	}

	// Set editor initial state
	if ($('#deal_editor_form').length > 0) {
		do_update_timed();

		$('#deal_editor_form\\:deal_duration').change(function() {
			var dur = $(this).val();
			if (dur != 'c') {
				var conv = new AnyTime.Converter({
					format: internal_format
				});
				var start = conv.parse($('#deal_editor_form\\:deal_startTime').val()).getTime();
				var ms_add = dur * 24 * 60 * 60 * 1000;
				var end = start + ms_add;
				var d = new Date();
				d.setTime(end);
				cur_endTime = conv.format(d);
				$('#deal_editor_form\\:deal_endTime').val( cur_endTime ).change();
			}
		});

		$('#deal_editor_form\\:deal_endTime').change(function() {
			if ($(this).val() != cur_endTime) {
				$('#deal_editor_form\\:deal_duration').val("c");
			}
		});

		$("#deal_editor_form\\:deal_startTime").AnyTime_picker(
		{
			format: internal_format,
			placement: "popup"
		});
		$("#deal_editor_form\\:deal_endTime").AnyTime_picker(
		{
			format: internal_format,
			placement: "popup"
		});

	}

	// Start timer for front page ticker
	if ($('#frontpage_deals').length > 0) {
		$('#frontpage_deals').bind("mouseover", function() {
			toggle_enabled = false;
		});
		$('#frontpage_deals').bind("mouseout", function() {
			toggle_enabled = true;
		});
		timerID = window.setInterval(toggle, 5000);
	}

	// Prepare the navigation menu
	$(" .menu ul ").css({
		display: "none"
	}); // Opera Fix
	$(" .menu li").hover(function(){
		$(this).find('ul:first')
		.css({
			visibility: "visible",
			display: "none"
		})
		.slideDown(200);
	},function(){
		$(this).find('ul:first')
		.css({
			visibility: "hidden"
		});
	});


});
var cur_endTime;
var internal_format = "%c/%d/%Z %H:%i";

var latest = true;
var toggle_enabled = true;
var toggle = function() {
	if (toggle_enabled) {
		if (latest) {
			$('#frontpage_deals_latest').fadeOut(500);
			$('#frontpage_deals_ending').fadeIn(500);
			latest = false;
		} else {
			$('#frontpage_deals_ending').fadeOut(500);
			$('#frontpage_deals_latest').fadeIn(500);
			latest = true;
		}
	}
}

function do_update_timed() {
	var timed = $('#deal_editor_form\\:deal_timed').is(':checked');
	if (timed) {
		$('#timed_deal_params').slideDown(400);
		$('#deal_editor_form\\:deal_minQty').removeAttr('disabled');
		$('#deal_editor_form\\:deal_maxQty').removeAttr('disabled');
		$('#deal_editor_form\\:deal_endTime').removeAttr('disabled');
		$('#deal_editor_form\\:deal_duration').removeAttr('disabled');
	} else {
		$('#timed_deal_params').slideUp(400);
		$('#deal_editor_form\\:deal_minQty').attr('disabled', 'disabled');
		$('#deal_editor_form\\:deal_maxQty').attr('disabled', 'disabled');
		$('#deal_editor_form\\:deal_endTime').attr('disabled', 'disabled');
		$('#deal_editor_form\\:deal_duration').attr('disabled', 'disabled');
	}
}

function set_deal_image_url(url) {
	$('#deal_image').attr('src', url);
}

function do_check_save() {
	var conv = new AnyTime.Converter({
		format: internal_format
	});
	var start = conv.parse($('#deal_editor_form\\:deal_startTime').val()).getTime();
	if (start <= new Date().getTime()) {
		this.show_confirm_dialog(
			"<img src=\"../resources/images/icons/32/warn.png\" alt=\"w\" title=\"Warning\" style=\"vertical-align: middle;\" />\n<p style=\"line-height: 28px;\">You have set the start time for this deal so that it will start immediately!\n<br />This will permanently lock the deal from being edited!\n<br />\nAre you sure you want to save this deal?\n</p>",
			"Question",
			do_save_action);
	} else {
		do_save_action();
	}
}

var do_save_action = function() {
	var fireOnThis = $("#deal_editor_form\\:deal_save_action")[0];
	if (document.createEvent)
	{
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( 'click', true, false );
		fireOnThis.dispatchEvent(evObj);
	}
	else if (document.createEventObject)
	{
		fireOnThis.fireEvent('onclick');
	}
}

function do_confirm_delete_client(client_id) {
	show_confirm_dialog(
		"<img src=\"/DailyMarket/resources/images/icons/32/user_delete.png\" alt=\"q\" title=\"Question\" style=\"vertical-align: middle;\" />\nAre you sure you want to delete the client <span style=\"font-weight: bold;\">" + client_id + "</span>?",
		"Question",
		function() {
			var fireOnThis = $('.del-client-command[title="'+client_id+'"]')[0];
			if (document.createEvent)
			{
				var evObj = document.createEvent('MouseEvents');
				evObj.initEvent( 'click', true, false );
				fireOnThis.dispatchEvent(evObj);
			}
			else if (document.createEventObject)
			{
				fireOnThis.fireEvent('onclick');
			}
		});
}

function do_confirm_delete_deal(deal_name) {
	show_confirm_dialog(
		"<img src=\"/DailyMarket/resources/images/icons/32/script_delete.png\" alt=\"q\" title=\"Question\" style=\"vertical-align: middle\" />\nAre you sure you want to cause the deal <span style=\"font-weight: bold;\">" + deal_name + "</span> to expire?",
		"Question",
		function() {
			var fireOnThis = $('#deal_manager_form\\:delete-deal')[0];
			if (document.createEvent)
			{
				var evObj = document.createEvent('MouseEvents');
				evObj.initEvent( 'click', true, false );
				fireOnThis.dispatchEvent(evObj);
			}
			else if (document.createEventObject)
			{
				fireOnThis.fireEvent('onclick');
			}
		});
}

function do_confirm_delete_category(cat_id, cat_name) {
	show_confirm_dialog(
		"<img src=\"/DailyMarket/resources/images/icons/32/folder_delete.png\" alt=\"q\" title=\"Question\" style=\"vertical-align: middle\" />\nAre you sure you want to delete the category <span style=\"font-weight: bold;\">" + cat_name + "</span>?",
		"Question",
		function() {
			var fireOnThis = $('.del-category-command[title="'+cat_id+'"]')[0];
			if (document.createEvent)
			{
				var evObj = document.createEvent('MouseEvents');
				evObj.initEvent( 'click', true, false );
				fireOnThis.dispatchEvent(evObj);
			}
			else if (document.createEventObject)
			{
				fireOnThis.fireEvent('onclick');
			}
		});
}

// Confirm Dialog
var onconfirm_yes;
var onconfirm_no;
function show_confirm_dialog(messageStr, title, onconfirmed, oncancelled) {

	// Display Title and Message
	$('#confirm-dialog-title').html(title);
	$('#confirm-dialog-message').html(messageStr);

	// Set handlers
	if (onconfirmed != undefined) onconfirm_yes = onconfirmed;
	if (oncancelled != undefined) onconfirm_no = oncancelled;

	// Show dialog
	$("#confirm-container").fadeIn(100);
	var top = ($(window).height() / 2 - $("#confirm-dialog").height() / 2);
	//if (top > 100) top = 100;
	if (top < 0) top = 0;
	var left = ($(window).width() / 2 - $("#confirm-dialog").width() / 2);
	if (left < 0) left = 0;
	$('#confirm-dialog').css('left', left + 'px');
	$('#confirm-dialog').css('top', top + 'px');
}
function close_confirm_dialog() {
	// Close and reset
	$("#confirm-container").fadeOut(100, function() {
		$("#confirm-dialog-title").html("Loading...");
		$("#confirm-dialog-message").html('<div class="abet-loading">Loading...</div>');
	});
	onconfirm_yes = undefined;
	onconfirm_no = undefined;
}
function confirm_pos() {
	if (onconfirm_yes != undefined) onconfirm_yes();
	close_confirm_dialog();
}
function confirm_neg() {
	if (onconfirm_no != undefined) onconfirm_no();
	close_confirm_dialog();
}

// Custom Dialog
function show_dialog(dialog_id) {
	// Show dialog
	$('#' + dialog_id + '-dialog-container').fadeIn(100);
	var top = ($(window).height() / 2 - $('#' + dialog_id + '-dialog').height() / 2);
	if (top < 0) top = 0;
	var left = ($(window).width() / 2 - $('#' + dialog_id + '-dialog').width() / 2);
	if (left < 0) left = 0;
	$('#' + dialog_id + '-dialog').css('left', left + 'px');
	$('#' + dialog_id + '-dialog').css('top', top + 'px');

}
function close_dialog(dialog_id) {
	// Reset Dialog
	$('#' + dialog_id + '-dialog-container').fadeOut(100);
}